﻿using GuanQL.Core.Domain.Medias;
using GuanQL.Core.Stores;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using ZhongJia.Core;
using ZhongJia.Core.Extensions;

namespace GuanQL.DataMigration.Seeds
{
    public abstract class BaseSeed : ISeeder
    {
        protected GQLDbContext _dbContext;
        protected Guid TenantId = new Guid("a3a4a4ec-5569-4b9b-f1fb-39dc32a6e5e1");

        public BaseSeed(
            GQLDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public abstract Task Seed();

        protected IEnumerable<Picture> GetRootPicture(string root, Guid tenantId, bool isSystem = false, PictureType type = PictureType.Avatar)
        {
            foreach (var filePath in Directory.GetFiles(root))
            {
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true))
                {
                    yield return new Picture
                    {
                        FileName = Path.GetFileName(filePath),
                        MimeType = Path.GetExtension(filePath.MapNameToMimeType()),
                        Type = type,
                        IsSystem = isSystem,
                        TenantId = tenantId,
                        IsTransient = false,
                        PictureStorage = new PictureStorage
                        {
                            Data = stream.ToByteArray()
                        }
                    };
                };
            }
        }
    }
}
